// pages/list/list.js
const config = require('../../config.js')
const http = require('../../http.js')
const util = require('../../utils/util.js')
var page = 1
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isRefresh: false,
    type: 0,
    cid: 0,
    meetingsData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      cid: options.id
    })
    page = 1
    this.getMeetings(page)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  keyInput: function (e) {
    this.setData({
      key: e.detail.value
    })
  },

  keySearch: function () {
    if (this.data.key != null && this.data.key != "") {
      console.log("keySearch")
      page = 1
      this.getMeetings(page, true)
    } else {
      wx.showToast({
        title: '搜索名称不能为空',
        icon: 'none'
      })
    }

  },

  typeClick: function (e) {
    let t = parseInt(e.currentTarget.dataset.type)
    this.setData({
      type: t
    })
    page = 1
    this.getMeetings(page)
  },
  toHear: function (e) {
    wx.navigateTo({
      url: '../hear/hear?roomId='+e.currentTarget.dataset.id,
    })
  },

  toJoin: function (e) {
    wx.navigateTo({
      url: '../room/room?roomId='+e.currentTarget.dataset.id,
    })
  },

  bindscrolltolower: function () {
    console.log("bindscrolltolower")
    page++
    this.getMeetings(page)
  },

  bindrefresherrefresh: function () {
    console.log("bindrefresherrefresh")
    this.setData({
      isRefresh: true
    })
    page = 1
    this.getMeetings(page)
  },

  getMeetings: function (p, isKey) {
    var that = this
    let params = null
    if (isKey) {
      params = {
        page: p,
        pageSize: 10,
        orderType: this.data.type,
        conferenceId: this.data.cid,
        meetingTitle: this.data.key
      }
    } else {
      params = {
        page: p,
        pageSize: 10,
        orderType: this.data.type,
        conferenceId: this.data.cid,
      }
    }
    http.request(
      config.meetings,
      'POST', params,
      function (res) {
        if (res.data != null) {
          if (p == 1) {
            that.setData({
              meetingsData: res.data
            })
          } else {
            that.setData({
              meetingsData: that.data.meetingsData.concat(res.data)
            })
          }
        }
        if (that.data.isRefresh) {
          that.setData({
            isRefresh: false
          })
        }
      },
      function (err) {
        wx.showToast({
          title: err,
          icon: 'none',
        })
      },true
    )
  }
})